package com.bxmdm.ssoapp1.controller;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.sso.SaSsoHandle;
import cn.dev33.satoken.sso.SaSsoUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.json.JSONUtil;
import com.ejlchina.okhttps.OkHttps;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * Sa-Token-SSO Client端 Controller
 */
@RestController
public class SsoClientController {

	// 前后端不分离的登录页面
	@RequestMapping("/")
	public String index() {
		String str = "<h2>Sa-Token SSO-Client 应用端</h2>" +
				"<p>当前会话是否登录：" + StpUtil.isLogin() + "</p>" +
				"<p><a href=\"javascript:location.href='/sso/login?back=' + encodeURIComponent(location.href);\">登录</a> " +
				"<a href='/sso/logout?back=self'>注销</a></p>";
		return str;
	}

	/*
	 * SSO-Client端：处理所有SSO相关请求
	 *         http://{host}:{port}/sso/login          -- Client端登录地址，接受参数：back=登录后的跳转地址
	 *         http://{host}:{port}/sso/logout         -- Client端单点注销地址（isSlo=true时打开），接受参数：back=注销后的跳转地址
	 *         http://{host}:{port}/sso/logoutCall     -- Client端单点注销回调地址（isSlo=true时打开），此接口为框架回调，开发者无需关心
	 */
	@RequestMapping("/sso/*")
	public Object ssoRequest(HttpServletRequest request) {
		System.out.println("请求主机IP"+request.getRemoteAddr()+"请求地址:"+request.getRequestURI()+";请求参数:"+ JSONUtil.toJsonStr(request.getParameterMap()));
		return SaSsoHandle.clientRequest();
	}


	// 配置SSO相关参数
	@Autowired
	private void configSso(SaTokenConfig cfg) {
		// ... 其他代码

		// 配置 Http 请求处理器
		cfg.sso.setSendHttp(url -> {
			return OkHttps.sync(url).get().getBody().toString();
		});
	}

	// 查询我的账号信息
	@RequestMapping("/sso/myinfo")
	public Object myinfo() {
		Object userinfo = SaSsoUtil.getUserinfo(StpUtil.getLoginId());
		System.out.println("--------info：" + userinfo);
		return userinfo;
	}

}


